package edu.udel.libra.controller;

import java.util.logging.Logger;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import edu.udel.libra.model.User;

public class AbstractController 
{
	private static final Logger LOG = Logger.getLogger(AbstractController.class.getName());

	/* Get User */
	protected User getUser() {
		// TODO: Create User from UserDetails
		User user = new User();
		user.setId(1);
		user.setEmail("jonli@udel.edu");
		
		return user;
	}
	
	/* Get user details */
	protected UserDetails getUserDetails() {
        UserDetails userDetails = null;
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (!(auth instanceof AnonymousAuthenticationToken))
        	userDetails = (UserDetails) auth.getPrincipal();
        
        LOG.info("getUserDetails(): " + userDetails);
        
        return userDetails;
    }
	
	/* Returns true if user is logged in */
    protected boolean isAuthenticated() {
    	UserDetails details =  getUserDetails();
    	
    	//LOG.info("isAuthenticated(): " + !(details == null));
    	
    	return !(details == null);
    }

}
